<?php
/**
 * [weliam] Copyright (c) 2016/3/23
 * index.ctrl
 * 全部商品列表控制器
 */
defined('IN_IA') or exit('Access Denied');

$ops = array('cabinet_admin','cabinet_admin_button','door_teacher','door_teacher_button','door_student','door_student_button');
$op = in_array($_GPC['op'], $ops) ? $_GPC['op'] : '';

global $_W;
$rolearray = array('admin','teacher','student');
$rolenamearray =array(
    'admin'=>'管理员',
    'teacher'=>'教师',
    'student'=>'课代表'
);
if($op =='cabinet_admin'){
    $role = $rolearray[0];
    //先判断当前role是否为admin
    $openid = $_W['openid'];
    $current_admin = pdo_get('homework_user', array('uniacid'=>$_W['uniacid'],'openid'=>$openid), array('id','name','role')); 
    if($current_admin['role'] != $role){
        $prompt = '您的角色不是管理员，无法使用解绑自身功能！';
        include wl_template('tips/prompt');
        exit;        
    }
    //得到该管理员管理的柜子id
    $current_cabinet = array();
    $all = pdo_getall('homework_cabinet_admin', array('uniacid'=>$_W['uniacid']), array('userid','device_info_id','device_name'));
    foreach($all as $key=>$value){
        $temp = explode(',',$value['userid']);
        if(in_array($current_admin['id'] , $temp))
            $current_cabinet[] = array(
                'info_id' => $value['device_info_id'],
                'device_name' => $value['device_name']
            );
    }
    //检查该数组，如果为空，则提示信息
    if(count($current_cabinet)==0){
        $prompt = '当前管理员并没有绑定任何柜子！';
        include wl_template('tips/prompt');
        exit;         
    }    
    //print_r($current_cabinet);
	include wl_template('unbindself/cabinet_admin');
    exit;    
}
if($op =='cabinet_admin_button'){
    $arr = $_POST['arr'];
    $userid = $_POST['userid'];
    $name = $_POST['name'];
    $data = '您已经成功解绑自身！解绑的柜子有：';
    if(count($arr)==0){
        print_r('您还未选择多选框');
        exit;
    } 
    foreach($arr as $key=>$value){
        $temp = pdo_get('homework_cabinet_admin', array('uniacid'=>$_W['uniacid'],'device_info_id'=>$value['info_id']), array('userid'));
        $temp_array = explode(',',$temp['userid']);
        foreach($temp_array as $k=>$val){
            if($val==$userid) unset($temp_array[$k]);
        }
        $temp_str = implode(',',$temp_array);
        
        $array = array('userid'=>$temp_str);
        pdo_update('homework_cabinet_admin' , $array ,array('uniacid'=>$_W['uniacid'],'device_info_id'=>$value['info_id']));
        
        $data .= $value['device_name'].'（ID为'.$value['info_id'].'）：'.$name;
        $data .='———';
    }
    $data = substr($data,0,strlen($str)-9);
    print_r($data);
}
if($op =='door_teacher'){
    $role = $rolearray[1];
    //先判断当前role是否为teacher
    $openid = $_W['openid'];
    $current_admin = pdo_get('homework_user', array('uniacid'=>$_W['uniacid'],'openid'=>$openid), array('id','name','role')); 
    if($current_admin['role'] != $role){
        $prompt = '您的角色不是教师，无法使用解绑自身功能！';
        include wl_template('tips/prompt');
        exit;        
    }
    //得到该教师管理的柜门信息，注意每个柜门只有一个教师
    $id_to_name = $this->get_id_to_name();
    $current_cabinet = array();
    $all = pdo_getall('homework_door_teacher', array('uniacid'=>$_W['uniacid'],'userid'=>$current_admin['id']), array('device_info_id','door'));
    foreach($all as $key=>$value){
        $current_cabinet[] = array(
            'info_id' => $value['device_info_id'],
            'device_name' => $id_to_name[$value['device_info_id']],
            'door' => $value['door']
        );
    }
    //检查该数组，如果为空，则提示信息
    if(count($current_cabinet)==0){
        $prompt = '当前教师并没有绑定任何柜门！';
        include wl_template('tips/prompt');
        exit;         
    }    
    // print_r($current_cabinet);
	include wl_template('unbindself/door_teacher');
    exit;     
}
if($op =='door_teacher_button'){//按钮后只是返回提示信息
    $arr = $_POST['arr'];
    $userid = $_POST['userid'];
    $name = $_POST['name'];
    $data = '您已经成功解绑自身！解绑的柜门有：';
    if(count($arr)==0){
        print_r('您还未选择多选框');
        exit;
    } 
    foreach($arr as $key=>$value){
        $array = array('userid'=>'');
        pdo_update('homework_door_teacher' , $array ,array('uniacid'=>$_W['uniacid'],'device_info_id'=>$value['info_id'],'door' => $value['door']));
        //老师解绑后，还需要对同一个柜门和学生们解绑
        pdo_update('homework_door_student' , $array ,array('uniacid'=>$_W['uniacid'],'device_info_id'=>$value['info_id'],'door' => $value['door']));
         
        $data .= $value['device_name'].'（ID为'.$value['info_id'].'）'.$value['door'].'号门：'.$name;
        $data .='———';
    }
    $data = substr($data,0,strlen($str)-9);
    print_r($data);
}
if($op =='door_student'){
    $role = $rolearray[2];
    //先判断当前role是否为student
    $openid = $_W['openid'];
    $current_student = pdo_get('homework_user', array('uniacid'=>$_W['uniacid'],'openid'=>$openid), array('id','name','role')); 
    if($current_student['role'] != $role){
        $prompt = '您的角色不是课代表，无法使用解绑自身功能！';
        include wl_template('tips/prompt');
        exit;        
    }
    //得到该课代表管理的柜门信息
    $id_to_name = $this->get_id_to_name();
    $current_cabinet = array();
    $all = pdo_getall('homework_door_student', array('uniacid'=>$_W['uniacid']), array('userid','device_info_id','door'));
    foreach($all as $key=>$value){
        $temp = explode(',',$value['userid']);
        if(in_array($current_student['id'] , $temp))
            $current_cabinet[] = array(
                'info_id' => $value['device_info_id'],
                'device_name' => $id_to_name[$value['device_info_id']],
                'door' => $value['door']
            );
    }
    //检查该数组，如果为空，则提示信息
    if(count($current_cabinet)==0){
        $prompt = '当前课代表并没有绑定任何柜门！';
        include wl_template('tips/prompt');
        exit;         
    }    
    // print_r($current_cabinet);
	include wl_template('unbindself/door_student');
    exit;     
}
if($op =='door_student_button'){
    $arr = $_POST['arr'];
    $userid = $_POST['userid'];
    $name = $_POST['name'];
    $data = '您已经成功解绑自身！解绑的柜门有：';
    if(count($arr)==0){
        print_r('您还未选择多选框');
        exit;
    } 
    foreach($arr as $key=>$value){
        $temp = pdo_get('homework_door_student', array('uniacid'=>$_W['uniacid'],'device_info_id'=>$value['info_id'],'door'=>$value['door']), array('userid'));
        $temp_array = explode(',',$temp['userid']);
        foreach($temp_array as $k=>$val){
            if($val==$userid) unset($temp_array[$k]);
        }
        $temp_str = implode(',',$temp_array);
        
        $array = array('userid'=>$temp_str);
        pdo_update('homework_door_student' , $array ,array('uniacid'=>$_W['uniacid'],'device_info_id'=>$value['info_id'],'door'=>$value['door']));
        
        $data .= $value['device_name'].'（ID为'.$value['info_id'].'）'.$value['door'].'号门：'.$name;
        $data .='———';
    }
    $data = substr($data,0,strlen($str)-9);
    print_r($data);    
}
